﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.Department
{
    /// <summary>
    /// ListView 的摘要说明。
    /// </summary>
    public partial class ListViewPage : Page
    {
        public static string DeptID;
        public static int DisplayType;

        private void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["DisplayType"] != null)
                {
                    DisplayType = Int32.Parse(Request.QueryString["DisplayType"]);
                }
                if (DisplayType == 0)
                {
                    //					lbOnline.BackColor = Color.FromArgb(0xCCCCCC);
                    //					lbOffLine.BackColor =Color.FromArgb(0xFFFFFF);	
                    cmdOnDepartment.Visible = false;
                    cmdOffDepartment.Visible = true;
                }
                else
                {
                    //					lbOnline.BackColor = Color.FromArgb(0xFFFFFF);
                    //					lbOffLine.BackColor =Color.FromArgb(0xCCCCCC);					
                    cmdOnDepartment.Visible = true;
                    cmdOffDepartment.Visible = false;
                }

                DeptID = Request.QueryString["DeptID"] ?? "1";

                //如果要求刷新部门树
                if (Request.QueryString["Refresh"] != null)
                {
                    Response.Write("<script language='javascript'>parent.contents.location.reload();</script>");
                }

                cmdOnDepartment.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人复职？');");
                cmdOffDepartment.Attributes.Add("Onclick", "javascript:return confirm('是否让选中的人离职？');");


                BindGrid();
            }
        }

        private void BindGrid()
        {
            var db = new Database();
            var prams = new[]
            {
                Database.MakeInParam("@Dept_ID", SqlDbType.Int, 4, DeptID),
                Database.MakeInParam("@Dimission", SqlDbType.Bit, 1, DisplayType)
            };
            DataTable dt;
            using (SqlDataReader dr = db.RunDataReader("dbo.sproc_GetStaffInDepartment", prams))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }

            dbStaffList.DataSource = dt.DefaultView;
            dbStaffList.DataBind();
        }

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dbStaffList.Items)
            {
                if (((CheckBox) item.FindControl(controlID)).Checked)
                {
                    selectedID += dbStaffList.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }

        private IList<int> GetSelectedItems(string controlID)
        {
            var list = new List<int>();
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dbStaffList.Items)
            {
                if (((CheckBox) item.FindControl(controlID)).Checked)
                {
                    list.Add(int.Parse(dbStaffList.DataKeys[item.ItemIndex].ToString()));
                }
            }
            return list.AsReadOnly();
        }

        private void lbOffLine_Click(object sender, EventArgs e)
        {
            Server.Transfer("Listview.aspx?DeptID=" + DeptID + "&DisplayType=1");
        }

        private void lbOnline_Click(object sender, EventArgs e)
        {
            Server.Transfer("Listview.aspx?DeptID=" + DeptID + "&DisplayType=0");
        }

        private void cmdNewStaff_Click(object sender, EventArgs e)
        {
            Server.Transfer("NewStaff.aspx?DeptID=" + DeptID + "&DisplayType =0");
        }

        private void cmdDepartmentOperate_Click(object sender, EventArgs e)
        {
            Server.Transfer("ODepartment.aspx?DeptID=" + DeptID);
        }

        private void cmdSetRight_Click(object sender, EventArgs e)
        {
            Server.Transfer("../AssignRule/RightListview.aspx?ObjID=" + DeptID + "&displayType=" + (int)EntityType.Position);
        }

        private void cmdOnDepartment_Click(object sender, EventArgs e)
        {
            string selectedID = GetSelectedItemID("Staff_ID");
            var db = new Database();
            if (selectedID.Trim() != "")
            {
                var prams = new[]
                {
                    Database.MakeInParam("@StaffIDS", SqlDbType.NVarChar, 300, selectedID)
                };
                db.RunProc("dbo.sproc_StaffRehab", prams);
            }
            Response.Redirect("Listview.aspx?DeptID=" + DeptID + "&displayType=" + DisplayType);
        }

        private void cmdOffDepartment_Click(object sender, EventArgs e)
        {
            IList<int> list = GetSelectedItems("Staff_ID");
            if (list.Count > 0)
            {
                if (Staff.Dimission(list))
                {
                    //Response.Write("<script language=javascript>alert('离职成功！');</script>");		
                    BindGrid();
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要离职的人员！');</script>");
            }
        }

        private void cmdChangeDepartment_Click(object sender, EventArgs e)
        {
            string selectedID = GetSelectedItemID("Staff_ID");
            if (selectedID.Trim() != "")
            {
                Response.Redirect("ChangeDepartment.aspx?DeptID=" + DeptID + "&StaffIDS=" + selectedID + "&DisplayType=" + DisplayType + "&BackFilePath=" + Request.CurrentExecutionFilePath);
            }
            else
            {
                Response.Write("<script language=javascript>alert('请选择要调职的人员！');</script>");
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lbOnline.Click += new System.EventHandler(this.lbOnline_Click);
            this.lbOffLine.Click += new System.EventHandler(this.lbOffLine_Click);
            this.cmdNewStaff.Click += new System.EventHandler(this.cmdNewStaff_Click);
            this.cmdDepartmentOperate.Click += new System.EventHandler(this.cmdDepartmentOperate_Click);
            this.cmdSetRight.Click += new System.EventHandler(this.cmdSetRight_Click);
            this.cmdOnDepartment.Click += new System.EventHandler(this.cmdOnDepartment_Click);
            this.cmdOffDepartment.Click += new System.EventHandler(this.cmdOffDepartment_Click);
            this.cmdChangeDepartment.Click += new System.EventHandler(this.cmdChangeDepartment_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dbStaffList.CurrentPageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion
    }
}